package com.ruoyi.academic.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.academic.domain.StuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.academic.domain.StudentAcademicInfo;
import com.ruoyi.academic.service.IStudentAcademicInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生学业信息Controller
 *
 * @author dongyv
 * @date 2025-05-15
 */
@RestController
@RequestMapping("/academic/info")
public class StudentAcademicInfoController extends BaseController
{
    @Autowired
    private IStudentAcademicInfoService studentAcademicInfoService;

    /**
     * 查询学生学业信息列表
     */
    @PreAuthorize("@ss.hasPermi('academic:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudentAcademicInfo studentAcademicInfo)
    {

        String teacherId=getUsername();
        if("admin".equals(teacherId))
        {
            //管理员
            startPage();
            List<StudentAcademicInfo> list = studentAcademicInfoService.selectStudentAcademicInfoList(studentAcademicInfo);
            return getDataTable(list);
        }
        else
        {
            //学生
            startPage();
            List<StudentAcademicInfo> list = studentAcademicInfoService.selectStudentAcademicInfoList_student(teacherId);
            return getDataTable(list);
        }


    }

    /**
     * 导出学生学业信息列表
     */
    @PreAuthorize("@ss.hasPermi('academic:info:export')")
    @Log(title = "学生学业信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentAcademicInfo studentAcademicInfo)
    {
        String teacherId=getUsername();
        if("admin".equals(teacherId))
        {
            //管理员
            List<StudentAcademicInfo> list = studentAcademicInfoService.selectStudentAcademicInfoList(studentAcademicInfo);
            ExcelUtil<StudentAcademicInfo> util = new ExcelUtil<StudentAcademicInfo>(StudentAcademicInfo.class);
            util.exportExcel(response, list, "学生学业信息数据");
        }
        else
        {
            //学生
            List<StudentAcademicInfo> list = studentAcademicInfoService.selectStudentAcademicInfoList_student(teacherId);
            ExcelUtil<StudentAcademicInfo> util = new ExcelUtil<StudentAcademicInfo>(StudentAcademicInfo.class);
            util.exportExcel(response, list, "学生学业信息数据");
        }
    }

    /**
     * 获取学生学业信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('academic:info:query')")
    @GetMapping(value = "/{serialNumber}")
    public AjaxResult getInfo(@PathVariable("serialNumber") Long serialNumber)
    {
        return success(studentAcademicInfoService.selectStudentAcademicInfoBySerialNumber(serialNumber));
    }

    /**
     * 新增学生学业信息
     */
    @PreAuthorize("@ss.hasPermi('academic:info:add')")
    @Log(title = "学生学业信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentAcademicInfo studentAcademicInfo)
    {
        return toAjax(studentAcademicInfoService.insertStudentAcademicInfo(studentAcademicInfo));
    }

    /**
     * 修改学生学业信息
     */
    @PreAuthorize("@ss.hasPermi('academic:info:edit')")
    @Log(title = "学生学业信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentAcademicInfo studentAcademicInfo)
    {
        return toAjax(studentAcademicInfoService.updateStudentAcademicInfo(studentAcademicInfo));
    }

    /**
     * 删除学生学业信息
     */
    @PreAuthorize("@ss.hasPermi('academic:info:remove')")
    @Log(title = "学生学业信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{serialNumbers}")
    public AjaxResult remove(@PathVariable Long[] serialNumbers)
    {
        return toAjax(studentAcademicInfoService.deleteStudentAcademicInfoBySerialNumbers(serialNumbers));
    }

    @GetMapping("/stu")
    public ResponseEntity<List<StuVO>> getByStudentId(Authentication authentication){
        String studentId = authentication.getName();
        List<StuVO> list = studentAcademicInfoService.selectByStudentId(studentId);
        return ResponseEntity.ok(list);
    }
}
